package com.bdqn.service;

import com.bdqn.mapper.SysUserMapper;
import com.bdqn.pojo.SysUser;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户功能模块业务实现类
 *
 */
@Service("sysUserService")
public class SysUserServiceImpl implements SysUserService {

    @Resource//@Resource
    private SysUserMapper sysUserMapper;

    /**
     * 分页查询用户列表
     */
    public List<SysUser> selectPageList(Integer pageNo, Integer pageSize, String realName, Integer roleId){
       return sysUserMapper.selectPageList(pageNo,pageSize,realName,roleId);
    }

    /**
     * 查询总记录数
     */
    @Override
    public int count(String realName,Integer roleId){
       return sysUserMapper.count(realName,roleId);
    }

    /**
     * 根据账号和密码查询用户
     *登录
     */
    @Override
    public SysUser login(String account, String password) {
        return sysUserMapper.getUser(account,password);
    }

    /**
     * 新增用户
     *
     * @param user
     * @return
     */
    @Override
    public boolean add(SysUser user) {
        boolean result = false;
        try{
            if(sysUserMapper.add(user) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 修改用户信息
     *
     * @param user
     * @return
     */
    @Override
    public boolean update(SysUser user) {
        boolean result = false;
        try{
            if(sysUserMapper.update(user) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 修改用户密码
     * @param id
     * @param password
     * @return
     */
    @Override
    public boolean upPWD(Integer id, String password){
        boolean result = false;
        try{
            if(sysUserMapper.upPWD(id,password) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 删除用户信息
     *
     * @param id
     * @return
     */
    @Override
    public boolean deleteUserById(Integer id) {
        boolean result = false;
        try{
            if(sysUserMapper.deleteUserById(id) == 1){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }

    /**
     * 根据ID查询用户信息
     *
     * @param id
     * @return
     */
    @Override
    public SysUser getId(Integer id){
       return sysUserMapper.getId(id);
    }

    /**
     * 检验账号是否重复
     *
     * @param account
     * @return
     */
    @Override
    public boolean getAccount(String account) {
        boolean result = false;
        try{
            if(sysUserMapper.getAccount(account) != null){
                result = true;
                //测试事务回滚时，打开注释
                //throw new RuntimeException();
            }
        }catch (RuntimeException e){
            e.printStackTrace();
            throw e;
        }
        return result;
    }
}
